<?php
namespace GrouppaGroup;
require_once 'Db.php';
require_once 'Planning.php';

class User
{
    /**
     * Hidden constructor, use getUser()
     */
    private function __construct()
    {
    }

    /**
     * name of the user
     * @var string
     */
    public $name;
    /**
     * Is the user online
     * @var boolean
     */
    public $online;
    /**
     * password of the user
     * @var string
     */
    public $password;
    /**
     * active group name of the user
     * @var string
     */
    public $activeGroup;
    /**
     * all groups to which the user is signed on
     * @var array array of group names (string)
     */
    public $allGroups;
    /**
     * id of the user
     * @var integer
     */
    public $id;
    /**
     * email adres of the user
     * @var string
     */
    public $email;
    /**
     * a plan specific to that user
     * @var Planning
     */
    public $plan;

    /**
     * cache for user data
     * @var array array of User
     */
    private static $userCache = array();

    /**
     * Gets user by his name
     * @param  string    $userName
     * @throws Exception if connection to database failed
     * @return User      instance of requested user, null if no such user
     */
    public static function getUser($userName)
    {
        //CHECK: return instance of requested user

        //first check if we already have it
        if (isset($userCache[$userName]))
            return $userCache[$userName];

        //fetch user data from database
        $result = Db::getInstance()->sendQuery("SELECT * FROM users,groups
                WHERE users.active_group_id = groups.id_group AND users.user_name = \"$userName\";");

        $user = null;
        if ($result->num_rows > 0) {
            $row = $result->fetch_assoc();

            $user = new User();
            $user->id = $row['id_user'];
            $user->name = $userName;
            $user->activeGroup = $row['group_name'];
            $user->online = self::is_online($row['last_online']);
            $date_online = $row['last_online'];

            $user->password = $row['password'];
            $user->allGroups = self::getGroups($user->id);
            $user->plan = self::getPlan($user->id);
        }

        $result->free();

        return $user;
    }

    /**
     * function to determine if user is still online based on his last time of action
     * @param  string  $date_online mysql formatted date of last action
     * @return boolean true iff user is still online
     */
    public static function is_online($date_online)
    {
        //CHECKED: check if user is online based on time since last server request
        if ($date_online) {
            $interval = strtotime('now') - $date_online;

            return $interval <= USER_ONLINE_TIME;
        } else

            return false;

    }

    /**
     * fetches groups to which the user is signed on
     * @param  integer         $userId id of the user
     * @return multitype:mixed array of strings with group names
     */
    private static function getGroups($userId)
    {
        //CHECK: get groups
        $resultGroups = Db::getInstance()->sendQuery("SELECT group_name FROM groups,usergroups
                WHERE usergroups.id_user = $userId AND usergroups.id_group = groups.id_group;");
        $result = array();
        while (($row = $resultGroups->fetch_array()) != null) {
            $result[] = $row[0];
        }

        $resultGroups->free();

        return $result;
    }

    /**
     * get plan of a user
     * @param  integer  $userId if of the user for which to get planning
     * @return Planning plan of the user
     */
    private static function getPlan($userId)
    {
        //TODO: fetch planning from the database
        return '';
    }

}
